package com.dgz.coolshark.controller;

import com.dgz.coolshark.mapper.BannerMapper;
import com.dgz.coolshark.pojo.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;


/**
 * @Author duguozheng
 * @Date 2023/04/27  14:53
 * @Description BannerController
 * @Version 1.0
 */
@RestController
@RequestMapping("/banner/")
public class BannerController {

    @Value("${imagesPath}")
    private String dirPath;

    @Autowired
    BannerMapper bannerMapper;

    @RequestMapping("select")
    public List<Banner> select() {
        return bannerMapper.select();
    }

    @RequestMapping("delete")
    public void delete(int id) {
        bannerMapper.deleteById(id);
    }
    @RequestMapping("insert")
    public void insert(String url) {
        bannerMapper.insert(url);
    }
    @RequestMapping("update")
    public void update(@RequestBody Banner banner) {
        //得到原来图片的路径
        String originUrl = bannerMapper.selectUrlById(banner.getId());
        //从本地删除原来的照片（数据库中存的只是图片url）
        new File(dirPath+originUrl).delete();
        bannerMapper.update(banner);
    }
    @RequestMapping("selectById")
    public Banner selectById(int id) {
        return bannerMapper.selectById(id);
    }
}
